package com.baozun.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

/**
 * 
 * @Title:
 * @Company:宝尊电子商务有限公司
 * @Description:
 * @Author:xiaozhou.zhou
 * @Since:2015年1月8日
 * @Copyright:Copyright (c) 2014
 * @ModifyDate:
 * @Version:1.1.0
 */
public class ReloadablePropertiesUtil {
    /**
     * properties要交给spring配置文件进行注入
     */
    ReloadableProperties properties;

    public void refreshCache() {
        properties.refresCache();
    }

    public ReloadableProperties getProperties() {
        return properties;
    }

    public void setProperties(ReloadableProperties properties) {
        this.properties = properties;
    }

    public String loadString(String key) {
        String value = null;
        try {
            value = properties.getProperty(key);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return value;
    }

    public Long loadLong(String key) {
        Long value = null;
        try {
            value = Long.valueOf(properties.getProperty(key));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return value;
    }

    public Integer loadInteger(String key) {
        Integer value = null;
        try {
            String s = properties.getProperty(key);
            value = Integer.valueOf(s);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return value;
    }

    public Collection<String> loadStringList(String key) {
        Collection<String> result = new ArrayList<String>();
        String s = null;
        try {
            s = properties.getProperty(key);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            String[] tmp = StringUtils.split(s, ",");
            if (tmp != null) {
                result = Arrays.asList(tmp);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    public Date loadDate(String key) {
        Date value = null;
        try {
            String s = properties.getProperty(key);
            if (s != null) {
                value = DateUtils.parseDate(s, new String[] {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"});
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return value;
    }

    public boolean loadBoolean(String key) {
        boolean value = false;
        try {
            String s = properties.getProperty(key);
            if (s != null) {
                value = Boolean.valueOf(s);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return value;
    }

}
